package io.github.shang11925.sbvuewelearn.controller;

import io.github.shang11925.sbvuewelearn.dto.VideoAttachmentsDTO;
import io.github.shang11925.sbvuewelearn.entity.VideoAttachments;
import io.github.shang11925.sbvuewelearn.response.VideoAttachmentsResponse;
import io.github.shang11925.sbvuewelearn.service.VideoAttachmentsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/attachments")
public class VideoAttachmentsController {

    @Autowired
    VideoAttachmentsService attachmentsService;

    @GetMapping("/list-video_attachments/{sectionId}")
    public VideoAttachmentsResponse list(@PathVariable Long sectionId){
        List<VideoAttachments> videoAttachments = attachmentsService.findBySectionId(sectionId);
        VideoAttachmentsResponse videoAttachmentsResponse = new VideoAttachmentsResponse();
        videoAttachmentsResponse.setVideoAttachments(videoAttachments);
        return videoAttachmentsResponse;

    }

    @PostMapping("/add-video_attachments")
    public void add(@RequestBody VideoAttachmentsDTO videoAttachmentsDTO, MultipartFile file) throws IOException {
        attachmentsService.uploadCover(file,videoAttachmentsDTO);
        attachmentsService.add(videoAttachmentsDTO);
    }
}